package com.orhon.smartcampus.modules.material.fetcher;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.material.dao.RoomDao;
import com.orhon.smartcampus.modules.material.entity.Floor;
import com.orhon.smartcampus.modules.material.entity.Room;
import com.orhon.smartcampus.modules.material.gqlModel.GQLFloor;
import com.orhon.smartcampus.modules.material.gqlModel.GQLRoom;
import com.orhon.smartcampus.modules.material.mm.FloorMapper;
import com.orhon.smartcampus.modules.material.mm.RoomMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class RoomFetcher implements DataFetcher {
	@Autowired
	RoomDao roomDao;
	
	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		int id = Integer.parseInt(env.getArgument("id").toString());

		String hql = "from Room where deleted_at = null and id = "+id;
		Room item =  roomDao.findId(hql, null);
		if(item!=null) {
			GQLFloor gqlFloor = null;
			if (env.getSelectionSet().contains("floor")){
				Floor floor = item.getMaterialFloorByFloorId();
				gqlFloor = FloorMapper.MAPPER.toFloor(floor);
			}
			
			GQLSchool GgqlSchool = null;
 			if (env.getSelectionSet().contains("school")){
 				Schools schools = item.getBaseSchoolsBySchoolId();
 				GgqlSchool = SchoolMapper.MAPPER.toSchool(schools);
 			}

			GQLRoom room = RoomMapper.MAPPER.toRoom(item);
			room.setSchool(GgqlSchool);
			room.setFloor(gqlFloor);
			return room;
		}
		return null;
	}
	
}
